package io.renren.modules.generator.service.impl;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.ProjectDao;
import io.renren.modules.generator.entity.ProjectEntity;
import io.renren.modules.generator.service.ProjectService;


@Service("projectService")
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectEntity> implements ProjectService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ProjectEntity> wrapper = new QueryWrapper<>();
        if (MapUtils.isNotEmpty(params)) {

            /**
             * 获取参数
             */
            String key = MapUtils.getString(params, "key");
            Integer myCompanyId = MapUtils.getInteger(params, "myCompany");
            Object idListObj = params.get("projectIdList");


            if (StringUtils.isNotBlank(key)) {
                wrapper.like("project_name", key);
            }

            if (myCompanyId != null) {
                wrapper.eq("company_id", myCompanyId);
            }

            if (idListObj != null) {
                List<Integer> projectIdList = (ArrayList<Integer>)idListObj;
                for (Integer projectId : projectIdList) {
                    wrapper.eq("id", projectId).or();
                }
            }
        }
        IPage<ProjectEntity> page = this.page(
                new Query<ProjectEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}